package org.misty.redis.autoconfigure.util;

import org.springframework.util.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Misty on 2020-06-11
 */
public class Util {
    private static final Pattern PATTERN = Pattern.compile("[_|-]+(\\w)");

    private Util() throws Exception {
        throw new Exception();
    }

    public static String camelize(String s) {
        if (s == null || "".equals(s)) {
            return null;
        }
        String initial;
        if (s.contains("_") || s.contains("-")) {
            s = s.toLowerCase();
            Matcher matcher = PATTERN.matcher(s);
            StringBuffer buf = new StringBuffer();
            while (matcher.find()) {
                matcher.appendReplacement(buf, matcher.group(1).toUpperCase());
            }
            matcher.appendTail(buf);
            return buf.toString();
        } else if ((initial = s.substring(0, 1)).equals(initial.toUpperCase())) {
            return initial.toLowerCase() + s.substring(1);
        }
        return s;
    }

    public static String orElse(String... args) {
        if (args != null) {
            for (String arg : args) {
                if (!StringUtils.isEmpty(arg)) {
                    return arg;
                }
            }
        }
        return null;
    }
}

